home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Problem Negating an Unsigned Char
- Date: 6 Mar 1996 00:34:52 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hjincINNpp0@keats.ugrad.cs.ubc.ca>
- References: <4he27sINNdel@keats.ugrad.cs.ubc.ca> <DnqMyr.4pF@cwi.nl> <4hfmmgINN4t8@anvil.ugrad.cs.ubc.ca> <Dntr8t.L41@cwi.nl>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <Dntr8t.L41@cwi.nl>, Dik T. Winter <dik@cwi.nl> wrote:
- >In article <4hfmmgINN4t8@anvil.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
- > > In article <DnqMyr.4pF@cwi.nl>, Dik T. Winter <dik@cwi.nl> wrote:
- > > > > To make it portable, you must manually mask for the lower eight bits:
- > > > >
- > > > > if (a == (~b & 0xff))
- > > >
- > > >And how would this work on machines with other than eight bit chars?
- > >
- > > Just fine, thank you. The C standard guarantees char to be at least eight
- > > bits wide. The original poster wants eight bit arithmetic; i.e. that the
- > > complement of 0x11 be equal to 0xEE. This is what he is testing for, and
- > > the above is how you get it.
- >
- >Well, that is your interpretation of what the original poster wants. See
- >the subject line. As I read it he just wanted to know why
- > a == ~b
- >does not work and why
- > a == (unsigned char)(~b)
- >does work.
- >...
- > > Casting to an unsigned quantity is implementation defined. Are you suggesting
- > > that the invocation of implementation defined behavior is more portable than a
- > > well-defined bit masking operation?
- >
- >I know that negating unsigned chars with CHAR_BIT bits is difficult to do
- >portably (see for instance Peter Seebach's posts). And, yes, I know that
- >casting is likely to fail on a 1's complement machine. But of the 1's
-
- Casting to a narrower unsigned type from a signed type does not fail. It is
- well-defined as doing a congruential operation (it finds the smallest positive
- residue of the signed number modulo the power of two that represents the
- unsigned quantity).
-
- What does fail is assuming that if you negate the bits of a signed integral
- type, you get a particular integer value, and hence a particular congruence
- class. That is what is the culprit in this problem, not the cast itself.
- --
-
-